/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jforum.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import jforum.domain.Message;
import jforum.domain.User;
import jforum.util.DBConnector;

/**
 *
 * @author Вадим
 */
public class MessageDAO implements BasicDAO<Message>{
    private DBConnector connector;

    public MessageDAO(DBConnector conn) {
        this.connector=conn;
    }

    private Message getFromResultSet(ResultSet rs) throws SQLException {
        User user=new User(rs.getInt("user_id"),rs.getString("login"));
        return new Message(rs.getInt("message_id"),rs.getInt("parent_id"), rs.getString("str"), user, rs.getLong("cr_time"), rs.getString("path"));
    }

    private List<Message> getListFromResultSet(ResultSet rs) throws SQLException {
        List<Message> list = new LinkedList<Message>();
        while (rs.next())
        {
            Message m = this.getFromResultSet(rs);
            list.add(m);
        }
        rs.close();
        return list;
    }

    public Message Load(int id) throws SQLException {
        Message m=null;
        ResultSet rs=this.connector.executePreparedQuery("SELECT * FROM \"tree_message\" m, \"user\" u "+
                "WHERE m.user_id=u.user_id AND m.message_id=?;", id);
        if (rs.next())
        {
            m=this.getFromResultSet(rs);
        }
        rs.close();
        return m;
    }

    public List<Message> LoadDescendants(int id) throws SQLException {
        List<Message> list = new LinkedList<Message>();
        ResultSet rs=this.connector.executePreparedQuery("SELECT q.*, nlevel(q.path), u.login FROM tree_message m, tree_message q, \"user\" u "+
        "WHERE q.path <@ m.path AND q.user_id=u.user_id AND m.message_id=? ORDER BY q.path;", id);

        while (rs.next())
        {
            Message m = this.getFromResultSet(rs);
            m.setLevel(rs.getInt("nlevel"));
            list.add(m);
        }
        rs.close();

        return list;
    }

    public List<Message> LoadByKeyword(String keyword) throws SQLException {
        ResultSet rs=this.connector.executePreparedQuery("SELECT * FROM \"tree_message\" m, \"user\" u "+
                "WHERE m.user_id=u.user_id AND m.str LIKE '%'||?||'%';", keyword);
        return getListFromResultSet(rs);
    }
    public List<Message> LoadForUser(int id) throws SQLException {
        ResultSet rs=this.connector.executePreparedQuery("SELECT * FROM \"tree_message\" m, \"user\" u "+
                "WHERE m.user_id=u.user_id AND u.user_id=?;", id);
        return getListFromResultSet(rs);
    }

    public List<Message> LoadForUser(String login) throws SQLException {
        ResultSet rs=this.connector.executePreparedQuery("SELECT * FROM \"tree_message\" m, \"user\" u "+
                "WHERE m.user_id=u.user_id AND u.login=?;", login);
        return getListFromResultSet(rs);
    }

    public List<Message> LoadForUserAndKeyword(String keyword, String login) throws SQLException {
        ResultSet rs=this.connector.executePreparedQuery("SELECT * FROM \"tree_message\" m, \"user\" u "+
                "WHERE m.user_id=u.user_id AND m.str LIKE '%'||?||'%' AND u.login=?;", keyword, login);
        return getListFromResultSet(rs);
    }

    public Message LoadOrCreate(int id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Message> LoadParents() throws SQLException {
        ResultSet rs=this.connector.executeQuery("SELECT * FROM \"tree_message\" m , \"user\" u WHERE m.user_id=u.user_id "
                +"AND m.parent_id IS NULL;");
        return getListFromResultSet(rs);
    }

    public List<Message> LoadList() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Message> LoadPage(int startRowIndex, int maximumRows) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Save(Message obj) throws SQLException {
        this.connector.executePreparedUpdate("INSERT INTO \"tree_message\""+
                " (user_id, str, cr_time) VALUES (?, ?, ?);", obj);
    }

    public void SaveAsDesc(Message obj, int id) throws SQLException {
        this.connector.executePreparedUpdate("INSERT INTO \"tree_message\""+
                " (user_id, parent_id, str, cr_time) VALUES (?, ?, ?, ?);", obj , id);
    }

    public void Update(Message obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Delete(Message obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Delete(int id) throws SQLException {
        this.connector.executePreparedUpdate("UPDATE \"tree_message\" SET str='<-- Сообщение удалено модератором -->' "+
                "WHERE message_id=?", id);
    }

    public void DeleteAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int GetCount() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
